<template>
    <SysDialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
        @onClose="onClose" @onConfirm="commit">
        <template v-slot:content>
            <el-form :model="addModel" ref="addFormRef" label-width="90px" :inline="false" size="default">
                <h5>投标信息</h5>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="touBiaoMingCheng" label="投标名称">
                            <el-input v-model="addModel.touBiaoMingCheng"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="touBiaoLeiXing" label="投标类型">
                            <el-input v-model="addModel.touBiaoLeiXing"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="jiHuiMingCheng" label="机会名称">
                            <el-input v-model="addModel.jiHuiMingCheng"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="jiHuiBianHao" label="机会编号">
                            <el-input v-model="addModel.jiHuiBianHao"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="touBiaoKeHu" label="投标客户">
                            <el-input v-model="addModel.touBiaoKeHu"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="xiangMuMingCheng" label="项目名称">
                            <el-input v-model="addModel.xiangMuMingCheng"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="jiShuFangAn" label="技术方案">
                            <el-input v-model="addModel.jiShuFangAn"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="baoJiaDan" label="报价单">
                            <el-input v-model="addModel.baoJiaDan"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <h5>机会信息</h5>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="customerNum" label="行业">
                            <el-input v-model="addModel.hangYe"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="jieDuan" label="阶段">
                            <el-input v-model="addModel.jieDuan"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="xiaoShouJiHuiRiQi" label="销售机会日期" label-width="120px">
                            <el-date-picker v-model="addModel.xiaoShouJiHuiRiQi" type="date" placeholder="选择日期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="customerNum" label="预算">
                            <el-input v-model="addModel.yuSuan"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="customerNum" label="线索来源">
                            <el-input v-model="addModel.xianSuoLaiYuan"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="customerNum" label="片区">
                            <el-input v-model="addModel.pianQu"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </template>
    </SysDialog>
</template>

<script setup lang="ts">

import SysDialog from '@/components/SysDialog.vue'
import useDialog from '@/hooks/useDialog';
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { addUserApi, editUserApi, getListApi } from '@/api/tb';
import { EditType, Title } from '@/type/BaseType';
import { TbModel } from '@/api/tb/TbModel';
const { dialog, onClose, onConfirm, onShow } = useDialog()    
const show = (type: string, row?: TbModel) => {
    
    type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT
    dialog.height = 500
    dialog.width = 1000
    dialog.visible = true

    if (row) {
        nextTick(() => {
            Object.assign(addModel, row)
            console.log('addModel:', addModel);
        })
    }

    addFormRef.value?.resetFields()

    addModel.type = type;
};

defineExpose({
    show
})

const addModel = reactive<TbModel>({
    type: '',
    id: '',
    hangYe: '',
    xiaoShouJiHuiRiQi: '',
    touBiaoMingCheng: '',
    touBiaoLeiXing: '',
    jiHuiMingCheng: '',
    jiHuiBianHao: '',
    touBiaoKeHu: '',
    xiangMuMingCheng: '',
    jiShuFangAn: '',
    baoJiaDan: '',
    jieDuan: '',
    yuSuan: '',
    xianSuoLaiYuan: '',
    pianQu: '',

})

const addFormRef = ref<FormInstance>()


const emists = defineEmits(['onFresh'])  


const commit = () => {
    addFormRef.value?.validate(async (valid) => {
        if (valid) {
            let res = null;
            if (addModel.type == EditType.ADD) {
                res = await addUserApi(addModel);
            } else {
                res = await editUserApi(addModel);
            }
            console.log(res)
            if (res && res.code == 200) {
                ElMessage.success(res.msg)
                emists('onFresh')
                onClose()
            }
        }
    })
}
</script>
<style scoped></style>